package com.clarencezero.mylover.controller;

import com.clarencezero.mylover.param.Result;
import com.clarencezero.mylover.param.entiry.LoginBean;
import com.clarencezero.mylover.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/api")
public class MemberController {
    @Autowired
    private MemberService memberService;
    Logger logger = LoggerFactory.getLogger(MemberController.class);
    @PostMapping("/user/login")
    @ResponseBody
    public String login(@RequestBody LoginBean loginBean) {
        System.out.println(loginBean.getPassword() + " : " + loginBean.getUsername());
        JSONObject object1 = new JSONObject();
        try {
            String jsonStr = "{\"token\":\"admin\"}";
            object1.put("code", 20000);
            object1.put("data", jsonStr);
            System.out.println(object1.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object1.toString();
    }

    @PostMapping("/user/sign")
    @ResponseBody
    public Result sign(@RequestBody LoginBean loginBean) {
        Result result = new Result();
        result = memberService.sign(loginBean);
        return result;
    }

    @GetMapping("/user/allUser")
    @ResponseBody
    public Result getAllUser() {
        Result result = new Result();

        result = memberService.getAllUser();
        return result;
    }


    @GetMapping("/user/info")
    @ResponseBody
    public String info() {
        String jsonStr = "{\n" +
                "    \"code\": 20000,\n" +
                "    \"data\": {\n" +
                "        \"roles\": [\n" +
                "            \"admin\"\n" +
                "        ],\n" +
                "        \"name\": \"admin\",\n" +
                "        \"avatar\": \"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif\"\n" +
                "    }\n" +
                "}";
        return jsonStr;
    }
}
